package com.dc.free.module.synchronization_zsedz_view.service;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dc.free.module.bgxm.db.model.VWxBgxm;
import com.dc.free.module.bgxm.service.VWxBgxmService;
import com.dc.free.module.hjdzxx.db.model.VWxHjdzxx;
import com.dc.free.module.hjdzxx.service.VWxHjdzxxService;
import com.dc.free.module.hjsfzm.db.model.VWxHjsfzm;
import com.dc.free.module.hjsfzm.service.VWxHjsfzmService;
import com.dc.free.module.lsrksfzm.db.model.VWxLsrksfzm;
import com.dc.free.module.lsrksfzm.service.VWxLsrksfzmService;
import com.dc.free.module.synchronization_zsedz_view.db.model.SynchronizationZsedzView;
import com.dc.free.module.zxhkxx.db.model.VWxZxhkxx;
import com.dc.free.module.zxhkxx.service.VWxZxhkxxService;
import com.jfinal.kit.StrKit;


public class SynchronizationZsedzViewScanner {
	
	//扫描间隔(单位毫秒)
	private static volatile int scanIntervalWithMsec = 1 * 1000;
	private static final ExecutorService service = Executors.newFixedThreadPool(20);
	//启动标识
	private static volatile boolean isRunning;
	
	private static void handler(SynchronizationZsedzView synchronizationZsedzView) {
		String request = synchronizationZsedzView.getRequest();
		Object response = new Object();
		
		if(StrKit.notBlank(request, synchronizationZsedzView.getCode())) {
			switch (synchronizationZsedzView.getCode()) {
			case "v_wx_bgxm":
				response = VWxBgxmService.get(JSON.parseObject(request, VWxBgxm.class));
				break;
			case "v_wx_hjdzxx":
				response = VWxHjdzxxService.get(JSON.parseObject(request, VWxHjdzxx.class));
				break;
			case "v_wx_hjsfzm":
				VWxHjsfzm vWxHjsfzm = VWxHjsfzmService.get(JSON.parseObject(request, VWxHjsfzm.class));
				if(!JSON.parseObject(request).getBooleanValue("zp")) {
					vWxHjsfzm.setZp(null);
				}
				response = vWxHjsfzm;
				break;
			case "v_wx_lsrksfzm":
				response = VWxLsrksfzmService.get(JSON.parseObject(request, VWxLsrksfzm.class));
				break;
			case "v_wx_zxhkxx":
				response = VWxZxhkxxService.get(JSON.parseObject(request, VWxZxhkxx.class));
				break;
			}
		}
		
		synchronizationZsedzView.setResponse(JSON.toJSONString(response)).setStatus(1).update();
		
	}
	
	/**
	 * 更新至待处理状态9
	 * @param list
	 */
	private static void updateWithUnhandlerStatus(List<SynchronizationZsedzView> list) {
		StringBuffer ids = new StringBuffer();
		
		for(SynchronizationZsedzView temp : list) {
			if(ids.length()==0) {
				ids.append(temp.getId());
			}
			else {
				ids.append(",").append(temp.getId());
			}
		}
		
		if(ids.length() > 0)
			SynchronizationZsedzViewService.update(new SynchronizationZsedzView().setStatus(9).setId(ids.toString()));
		
	}
	
	/**
	 * 启动扫描器
	 */
	final public static void start() {
		isRunning = true;
		
		//扫描
		service.execute(new Runnable() {
			@Override
			public void run() {
				while(isRunning) {
					service.execute(new Runnable() {
						@Override
						public void run() {
							SynchronizationZsedzView SynchronizationZsedzView = new SynchronizationZsedzView();
							SynchronizationZsedzView.setStatus(0);
							List<SynchronizationZsedzView> list = SynchronizationZsedzViewService.list(SynchronizationZsedzView);
							updateWithUnhandlerStatus(list);
							
							for(final SynchronizationZsedzView temp : list) {
								service.execute(new Runnable() {
									@Override
									public void run() {
										handler(temp);
									}
								});
							}
						}
					});
					
					try {
						Thread.sleep(scanIntervalWithMsec);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		
	}
	
	/**
	 * 停止扫描器
	 */
	final public static void stop() {
		isRunning = false;
	}
}